﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using com.huigo.Framework.ServiceUtils.Parser;
using System.Text;

namespace com.huigo.Admin.Web.order
{
    public partial class OrderList : BasePage
    {
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }

        private string queryOrderNo
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["orderNo"]);
            }
        }

        private string queryProName
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["proName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["proName"]);
            }
        }
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        private long queryType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["Type"]) ? long.MinValue : Convert.ToInt64(com.loto.Common.Utils.SqlEscape(Request.QueryString["Type"]));
            }
        }
        private int queryOrderType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderType"]) ? int.MinValue : Convert.ToUInt16(com.loto.Common.Utils.SqlEscape(Request.QueryString["orderType"]));
            }
        }
        private string queryProInsName
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["proInsName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["proInsName"]);
            }
        }
        private string queryCustName
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["custName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["custName"]);
            }
        }
        private long queryInsCompany
        {
            get
            {
                return com.loto.Common.Utils.IsNumeric(Request.QueryString["insCompany"]) ? Convert.ToInt64(com.loto.Common.Utils.SqlEscape(Request.QueryString["insCompany"])) : long.MinValue;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                BindData();
            }
        }

        protected void BindData()
        {
            orderName.Text = queryOrderNo;
            proName.Text = queryProName;
            proInsName.Text = queryProInsName;
            custName.Text = queryCustName;

            if (queryStart.ToString("yyyy-MM-dd") != string.Empty && queryEnd.ToString("yyyy-MM-dd") != string.Empty)
            {
                if (queryStart.ToString("yyyy-MM-dd") != "0001-01-01" && queryEnd.AddDays(1).ToString("yyyy-MM-dd") != "0001-01-02")
                {
                    beginDate.Text = queryStart.ToString("yyyy-MM-dd");
                    endDate.Text = queryEnd.ToString("yyyy-MM-dd");
                }
            }
            statusList.Text = queryOrderType.ToString();
            insList.Text = queryInsCompany.ToString();
            proType.Text = queryType.ToString();
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("orderNO", queryOrderNo));
            argList.Add(new RequestFuncParams("orderStart", queryStart.ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("orderEnd", queryEnd.AddDays(1).ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("orderType", queryOrderType.ToString()));
            argList.Add(new RequestFuncParams("insCompanyId", queryInsCompany.ToString()));
            argList.Add(new RequestFuncParams("proType", queryType.ToString()));
            argList.Add(new RequestFuncParams("policyNo", queryProName));
            argList.Add(new RequestFuncParams("policyInsName", queryProInsName));
            argList.Add(new RequestFuncParams("custName", queryCustName));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_ORDER_INFO, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                StringBuilder sb = new StringBuilder();
                sb.Append("<table><thead><tr><th style=\"width: 15%\">订单号</th><th style=\"width: 15%\">经销商</th><th style=\"width: 15%\">保险名称</th><th style=\"width: 15%\">被保人数</th><th style=\"width: 15%\">订单金额</th><th>订单时间</th><th>订单状态</th></tr></thead><tbody>");
                string content = "<tr><td><a href=\"OrderDetail.aspx?sid={7}&keepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=850\" title=\"订单详情- {0}\" class=\"thickbox\">{0}</a></td><td>{1}</td><td/>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td><div class=\"wrap_layout_track\"><a class=\"js_order_status_history\"  href=\"javascript:void(0);\" order_id=\"{7}\">{6}<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>订单操作记录</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div></td></tr>";
                for (int i = 0; i < parser.RowsCount; i++)
                {
                    sb.AppendFormat(content, parser["OrderNo", i].ToString(), parser["CustName", i].ToString(), parser["ProductName", i].ToString(), parser["OrderPersonCount", i].ToString(), ConvertCurrencyPay(Convert.ToDecimal(parser["OrderAmount", i].ToString())), Convert.ToDateTime(parser["OrderCreatedOn", i]).ToString("yyyy-MM-dd HH:mm"), parser["StatusDesc", i].ToString(), parser["OrderId", i].ToString());
                }
                sb.Append("</tbody></table>");
                LitOrder.Text = sb.ToString();
            }
            else
                LitOrder.Text = string.Empty;
        }
        private void LoadData()
        {
            //订单状态
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictOrderStatus);
            if (statusDropList != null)
            {
                statusList.DataSource = statusDropList;
                statusList.DataValueField = "Id";
                statusList.DataTextField = "Value";
                statusList.DataBind();
            };

            //保险公司列表
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_INS_COMPANY_DROPLIST, argList);

            List<DropList> cDropList = Tools.ParserToDropList(parser, "CompanyId", "CompanyShortName");
            if (cDropList != null)
            {
                insList.DataSource = cDropList;
                insList.DataValueField = "Id";
                insList.DataTextField = "Value";
                insList.DataBind();
            }

            ////保单类型

            List<DropList> tDropList = Tools.DictToDropList(Dict.DictInsType);
            if (tDropList != null)
            {
                proType.DataSource = tDropList;
                proType.DataValueField = "Id";
                proType.DataTextField = "Value";
                proType.DataBind();
            };
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "OrderList.aspx?";
            url += "page=1";
            if (orderName.Text != string.Empty)
                url += "&orderNo=" + orderName.Text;
            if (proName.Text != string.Empty)
                url += "&proName=" + proName.Text;
            if (beginDate.Text != string.Empty)
                url += "&Start=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (proType.SelectedValue != string.Empty)
                url += "&Type=" + proType.SelectedValue;
            if (proInsName.Text != string.Empty)
                url += "&proInsName=" + proInsName.Text;
            if (custName.Text != string.Empty)
                url += "&custName=" + custName.Text;
            if (insList.SelectedItem.Text != string.Empty)
                url += "&insCompany=" + insList.SelectedValue;
            if (statusList.SelectedValue != string.Empty)
                url += "&orderType=" + statusList.SelectedValue;
            Response.Redirect(url);
        }

        private string GetPagerUrl()
        {
            string url = "OrderList.aspx?";
            url += "page={0}";
            if (queryOrderNo != string.Empty)
                url += "&orderNo=" + queryOrderNo;
            if (queryProName != string.Empty)
                url += "&proName=" + queryProName;
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart.ToString("yyyy-MM-dd");
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd.ToString("yyyy-MM-dd");
            if (queryType != long.MinValue)
                url += "&Type=" + queryType;
            if (queryProInsName != string.Empty)
                url += "&proInsName=" + queryProInsName;
            if (queryCustName != string.Empty)
                url += "&custName=" + queryCustName;
            if (queryInsCompany != long.MinValue)
                url += "&insCompany=" + queryInsCompany;
            if (queryOrderType != int.MinValue)
                url += "&orderType=" + queryOrderType;
            return url;
        }
    }

}
